﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using InternetBankingG6Services.DAL;
namespace InternetBankingG6Services
{
    /// <summary>
    /// Summary description for BankingServices
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class BankingServices : System.Web.Services.WebService
    {
        BankDBDataContext db = new BankDBDataContext();
        [WebMethod]
        public long Inquery(int accountID)
        {
            return db.spGetBalanceByAccountID(accountID).SingleOrDefault().Balance;
        }

        [WebMethod]
        /**
         * @param fromID is AccountID of Sender
         * @param toID is AccountID of Receiver
         * money is quatity of money that sender send to receiver
         * return 0 is succesful, 1 is sender have not enough money, 2 is receiver is not exist, 3 receiver is not active, 4 can't tranfer yourself
         * */
        public int Tranfer(int fromID, int toID, long money)
        {
            if (fromID == toID)
            {
                return 4;
            }
            if (money > Inquery(fromID))
            {
                return 1;
            }
            else
            {
                switch (CheckStatusAccount(toID))
                {
                    case 1:
                        return 3;
                    case 2:
                        return 2;
                    default:
                        db.spWithdraw(fromID, money);
                        db.spDeposit(toID, money);
                        db.spLogTrans(fromID, toID, money, 1, 0);
                        
                        break;
                }
            }

            return 0;
        }
        /**
         * @param accountID int
         * return 0 if account is exist and active, 1 is exist but not active, 2 is not exist
         * */
        [WebMethod]
        public int CheckStatusAccount(int accountID)
        {
            if (db.spCheckStatusAccount(accountID).SingleOrDefault() != null)
            {
                if (db.spCheckStatusAccount(accountID).SingleOrDefault().Active == 1)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            return 2;
        }

        [WebMethod]
        public int Login(string username, string password)
        {
            if (db.spGetAccountID(username, password).SingleOrDefault() != null)
            {
                return db.spGetAccountID(username, password).SingleOrDefault().AID;
            }
            return 0;
        }

        [WebMethod]
        public AccountDetail GetAccountDetail(int accountID)
        {
            return db.spGetAccountDetailByAccountID(accountID).FirstOrDefault();
        }

        [WebMethod]
        public List<HistoryTranModified> GetHistoryTrans(int accountID)
        {
            return db.spGetLogTransByAccountID(accountID).ToList();
             
        }

        [WebMethod]
        public List<HistoryTranModified> SearchHistoryTrans(int statusOption, int accountID, int byDateOption, string fromDate, string toDate)
        {
            return db.spSearchHisTrans(statusOption, accountID, byDateOption, fromDate, toDate).ToList();
        }
    }
}
